//
//  WKMnemnonInputCell.m
//  WuKongUsernameLogin
//
//  Created by tt on 2023/9/14.
//

#import "WKMnemnonInputCell.h"

@implementation WKMnemnonInputModel

- (Class)cell {
    return WKMnemnonInputCell.class;
}

@end

@interface WKMnemnonInputCell ()<UITextViewDelegate>

@property(nonatomic,strong) UITextView *textView;

@property(nonatomic,strong) UIButton *pasteBtn;

@property(nonatomic,strong) WKMnemnonInputModel *model;

@end

@implementation WKMnemnonInputCell

+ (CGSize)sizeForModel:(WKMnemnonInputModel *)model {
    return CGSizeMake(WKScreenWidth, 200.0f);
}

- (void)setupUI {
    [super setupUI];
    
    self.backgroundColor = [UIColor clearColor];
    
    [self.contentView addSubview:self.textView];
    [self.textView addSubview:self.pasteBtn];
}

- (void)refresh:(WKMnemnonInputModel *)model {
    [super refresh:model];
    self.model = model;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.textView.lim_size = CGSizeMake(self.lim_width - 30.0f, self.lim_height - 30.0f);
    self.textView.lim_left = 15.0f;
    self.pasteBtn.lim_top = self.textView.lim_height - self.pasteBtn.lim_height - 5.0f;
    self.pasteBtn.lim_right = self.textView.lim_width - self.pasteBtn.lim_width - 5.0f;
}

- (UITextView *)textView {
    if(!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.placeholder = LLang(@"请按顺序输入助记词，并使用空格分隔");
        _textView.font = [WKApp.shared.config appFontOfSize:16.0f];
        _textView.layer.masksToBounds = YES;
        _textView.layer.cornerRadius = 8.0f;
        _textView.backgroundColor = WKApp.shared.config.cellBackgroundColor;
        _textView.delegate = self;
    }
    return _textView;
}

- (UIButton *)pasteBtn {
    if(!_pasteBtn) {
        _pasteBtn = [[UIButton alloc] init];
        [_pasteBtn setTitle:LLang(@"粘贴") forState:UIControlStateNormal];
        [_pasteBtn setTitleColor:WKApp.shared.config.themeColor forState:UIControlStateNormal];
        [_pasteBtn sizeToFit];
        [_pasteBtn addTarget:self action:@selector(onPaste) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pasteBtn;
}

-(void) onPaste {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSString *pasteboardString = pasteboard.string;
    self.textView.text = pasteboardString;
    if(self.model.onChange) {
        self.model.onChange(self.textView.text);
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    if(self.model.onChange) {
        self.model.onChange(textView.text);
    }
}

@end
